﻿using UnityEngine;
using System.Collections;

namespace AssetLoad
{
    public class AtlasLoader : LoaderBase 
    {
        public override string GetPrefix()
        {
            return @"dialogHero/";
        }

        public IEnumerator LoadAtlas(string atlasName, LoadCompleteNotify callback) {

#if !FIX_RESOURCE
            string abName = GetPrefix() + atlasName;
            AssetBundleManager.Instance.LoadAssetBundle(abName);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(abName)) {
                yield return null;
            }
            if (callback != null) {
                callback();
            }
#else
            ResourceManager.Instance.LoadResource(GetPrefix() + atlasName);
            yield return 0;
            if (callback != null) {
                callback();
            }
#endif
        }

        public void UnloadAtlas(string atlasName) {
#if !FIX_RESOURCE
            AssetBundleManager.Instance.UnloadAssetBundle(GetPrefix() + atlasName);
#else
            ResourceManager.Instance.UnloadResource(GetPrefix() + atlasName);
#endif
        }

        public UIAtlas GetAtlas(string atlasName) {
#if !FIX_RESOURCE
            string abName = GetPrefix() + atlasName;
            string assetName = atlasName;

            var obj = AssetBundleManager.Instance.GetAssetFromBundle(abName, assetName, typeof(GameObject)) as GameObject;
            if (obj != null) {
                return obj.GetComponent<UIAtlas>();
            }
            return null;
#else
            var obj = ResourceManager.Instance.GetResource(GetPrefix() + atlasName) as GameObject;
            if (obj != null)
            {
                return obj.GetComponent<UIAtlas>();
            }
            return null;
#endif
        }

    }

}